import axios from "axios";
import Qs from "qs";

const salesOrder={
  namespaced: true,
  state:()=>{
    return{

    }
  },
  mutations:{

  },
  actions:{
    /**
     *  查询待接受订单
     */
    getOrderWait({ state, commit, rootState }){
      return new Promise((resolve, reject) => {
        axios.get('/sales_order/waitorder'
        ).then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    /**
     *  查询已接受待发货订单
     */
    getOrderAccepted({ state, commit, rootState }){
      return new Promise((resolve, reject) => {
        axios.get('/sales_order/acceptedorder'
        ).then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    /**
     *  查询已完成订单
     */
    getOrderFinish({ state, commit, rootState }){
      return new Promise((resolve, reject) => {
        axios.get('/sales_order/finishedorder'
        ).then(res=>{
          resolve(res.data);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    /**
     *  接单。参数：sub_order_id，item_id，item_num
     */
    acceptOrder({ state, commit, rootState },obj){
      return new Promise((resolve, reject) => {
        axios.post('/sales_order/accept',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },
    /**
     *  拒单
     */
    refuseOrder({ state, commit, rootState },sub_order_id){
      return new Promise((resolve, reject) => {
        axios.post('/sales_order/refuse',
          Qs.stringify({sub_order_id_list: sub_order_id},{arrayFormat:'repeat'}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },
    /**
     *  更改库存，参数item_id，item_num
     */
    modifyNum({ state, commit, rootState },obj){
      return new Promise((resolve, reject) => {
        axios.put('/supply/onsell/changenum',
          Qs.stringify(obj),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },
    /**
     *  发货
     */
    deliver({ state, commit, rootState },sub_order_id){
      return new Promise((resolve, reject) => {
        axios.post('/sales_order/deliver',
          Qs.stringify({sub_order_id:sub_order_id}),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }
        ).then(res=>{
          resolve();
        }).catch(e=>{
          reject(e);
        })
      })
    },

  }
}

export default salesOrder;
